﻿using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Engine;
using System;
using System.IO;
using Android.Content.PM;
namespace SCPAK2
{
    [Activity(Label = "3D模型预览", LaunchMode = LaunchMode.SingleTask)]
    public class ModelViewActivity : EngineActivity
    {
        public static ModelViewActivity Context;
        public static string ModelPath;
        public static string TexturePath;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Context = this;
            Bundle config = Intent.GetBundleExtra("file");
            ModelPath = config.GetString("fullname");
            string p1 = Path.GetDirectoryName(ModelPath);
            string f1 = Path.GetFileNameWithoutExtension(ModelPath);
            TexturePath = Path.Combine(p1, $"{f1}.png");
            try {
                Engine.Window.Frame += ScreensManager.Update;
                Engine.Window.Frame += ScreensManager.Draw;
                Engine.Window.Deactivated += () => { GC.Collect(); };
                Engine.Window.Run(1078, 720);
                Engine.Window.View.KeyPress += new EventHandler<View.KeyEventArgs>(OnPressed);
            }
            catch (Exception e){
                Tip("打开失败:"+e.Message);
            }

        }
        public void OnPressed(object obj, View.KeyEventArgs eventArgs)
        {
            if (eventArgs.KeyCode == Keycode.Back) {
                Engine.Window.Frame -= ScreensManager.Draw;
                Engine.Window.Frame -= ScreensManager.Update;
                Engine.Window.View.KeyPress -= OnPressed;
                Engine.Window.Close();
            }
        }
        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            menu.Add("纹理导入");
            menu.Add("退出");
            return base.OnCreateOptionsMenu(menu);
        }
        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ToString()) {
                case "退出":
                    {
                        OnPressed(null,new View.KeyEventArgs(true,Keycode.Back,null));
                        break;
                    }
                case "纹理导入":
                    {
                        Tip("例:将1.dae模型与1.png纹理放到同一目录，再打开模型可自动识别");
                        break;
                    }

            }
            return base.OnOptionsItemSelected(item);
        }
        public void Tip(string msg) {
            Toast.MakeText(this,msg,ToastLength.Long).Show();
        }

    }
}